import compression from 'vite-plugin-compression'

// 创建压缩插件的工厂函数
export default function createCompression(isBuild, env) {
    if (!isBuild) {
        return [];
    }
    const plugins = [];
    const { VITE_BUILD_COMPRESS } = env
    const compressList = VITE_BUILD_COMPRESS.split(',')
    if (compressList.includes('gzip')) {
        plugins.push(
            compression({
                algorithm: 'gzip',
                ext: '.gz',
                deleteOriginFile: true,
                threshold: 10240,
            })
        );
    }

    if (compressList.includes('brotli')) {
        plugins.push(
            compression({
                algorithm: 'brotliCompress',
                ext: '.br',
                deleteOriginFile: true,
                threshold: 10240,
            })
        );
    }
    return plugins;
}